package service

import (
	"douyin/models"
	"douyin/repository/mysql"
	"douyin/setup/settings"
	"go.uber.org/zap"
)

// Feed 查询多个视频
func Feed(latestTime int64, userId int64) (videoList []models.Video, nextTime int64, err error) {
	//1.查询Vid表中latestTime往前的30个视频vidIds，倒序
	vidIds, err := mysql.SelectVidIdsByTime(latestTime, settings.Conf.Feed)
	if err != nil {
		zap.L().Error("mysql.SelectVidIdsByTime failed", zap.Error(err))
		return
	}

	//2.videoList中最后一个视频的CreateTime作为返回值nextTime
	lastVidId := vidIds[0]
	nextTime, err = mysql.SelectTimeByVid(lastVidId)
	if err != nil {
		zap.L().Error("mysql.SelectTimeByVid failed", zap.Error(err))
		return
	}

	//3.查询视频的User,组装
	videoList, err = mysql.SelectVideos(vidIds, userId)
	if err != nil {
		zap.L().Error("mysql.SelectVideos failed", zap.Error(err))
		return
	}
	return
}
